// 等待DOM加载完成
document.addEventListener('DOMContentLoaded', function() {
    // 轮播图功能
    let slideIndex = 1;
    
    // 确保页面加载时就显示第一张幻灯片
    showSlides(slideIndex);

    // 自动轮播
    setInterval(function() {
        changeSlide(1);
    }, 5000);

    // 手动切换幻灯片
    window.changeSlide = function(n) {
        showSlides(slideIndex += n);
    }

    // 当前幻灯片
    window.currentSlide = function(n) {
        showSlides(slideIndex = n);
    }

    // 显示幻灯片
    function showSlides(n) {
        let i;
        let slides = document.getElementsByClassName("slide");
        let dots = document.getElementsByClassName("dot");
        
        // 循环显示
        if (n > slides.length) {slideIndex = 1}
        if (n < 1) {slideIndex = slides.length}
        
        // 隐藏所有幻灯片
        for (i = 0; i < slides.length; i++) {
            slides[i].style.display = "none";
        }
        
        // 移除所有点的活动状态
        for (i = 0; i < dots.length; i++) {
            dots[i].className = dots[i].className.replace(" active-dot", "");
        }
        
        // 显示当前幻灯片和活动点
        slides[slideIndex-1].style.display = "block";
        dots[slideIndex-1].className += " active-dot";
    }

    // 导航栏滚动效果
    const header = document.querySelector('.header');
    const nav = document.querySelector('.nav');
    const navLinks = document.querySelectorAll('.nav a');
    
    window.addEventListener('scroll', function() {
        if (window.scrollY > 50) {
            header.style.padding = '10px 0';
            header.style.boxShadow = '0 2px 10px rgba(0, 0, 0, 0.1)';
        } else {
            header.style.padding = '15px 0';
            header.style.boxShadow = '0 2px 10px rgba(0, 0, 0, 0.05)';
        }
    });

    // 平滑滚动到锚点
    navLinks.forEach(link => {
        link.addEventListener('click', function(e) {
            e.preventDefault();
            
            // 获取目标部分
            const targetId = this.getAttribute('href');
            const targetSection = document.querySelector(targetId);
            
            if (targetSection) {
                // 修正计算要滚动的位置
                const headerHeight = header.offsetHeight;
                // 添加额外的偏移量，确保不会被导航栏挡住
                const targetPosition = targetSection.getBoundingClientRect().top + window.pageYOffset - headerHeight - 10;
                
                // 平滑滚动
                window.scrollTo({
                    top: targetPosition,
                    behavior: 'smooth'
                });
                
                // 更新活动链接
                navLinks.forEach(link => link.classList.remove('active'));
                this.classList.add('active');
            }
        });
    });

    // 监听滚动以更新活动导航链接
    window.addEventListener('scroll', function() {
        const scrollPosition = window.pageYOffset;
        const headerHeight = header.offsetHeight + 20; // 添加额外的偏移量
        
        // 获取所有部分
        const sections = document.querySelectorAll('section[id]');
        
        sections.forEach(section => {
            const sectionTop = section.offsetTop - headerHeight;
            const sectionBottom = sectionTop + section.offsetHeight;
            const sectionId = '#' + section.getAttribute('id');
            
            if (scrollPosition >= sectionTop && scrollPosition < sectionBottom) {
                // 找到当前部分的导航链接并激活
                navLinks.forEach(link => {
                    link.classList.remove('active');
                    if (link.getAttribute('href') === sectionId) {
                        link.classList.add('active');
                    }
                });
            }
        });
    });

    // 表单提交事件
    const contactForm = document.querySelector('.contact-form form');
    if (contactForm) {
        contactForm.addEventListener('submit', function(e) {
            e.preventDefault();
            // 这里可以添加表单验证和提交逻辑
            alert('感谢您的留言！我们将尽快与您联系。');
            this.reset();
        });
    }

    // 添加简单的页面动画效果
    const fadeInElements = document.querySelectorAll('.section-title, .about-content, .brand-content, .timeline, .philosophy-item, .culture-item, .team-member, .tech-item');
    
    // 创建IntersectionObserver实例
    const observer = new IntersectionObserver((entries) => {
        entries.forEach(entry => {
            if (entry.isIntersecting) {
                entry.target.classList.add('fade-in');
                observer.unobserve(entry.target);
            }
        });
    }, {
        threshold: 0.2
    });
    
    // 观察所有元素
    fadeInElements.forEach(element => {
        observer.observe(element);
    });
}); 